var ns;
app = new Vue({
    el: '#app',
    data: {
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
        showtime:false,
        ProjectName:'',
        projectList: [], 
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            startTime: '',
            endTime: '',
            activeDate: 0,
            activeDateDesc: '时间',
            ProjectCompanyType: 1,
            ProjectID:0,
            ProjectIDs: [],
            StatusIDs:[],
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showTop:false,
        showsearch:false,
        StatusList:[
            {"ID":0,"Name":"全部"},
            {"ID":1,"Name":"未开始"},
            {"ID":4,"Name":"待执行"},
            {"ID":2,"Name":"巡检中"},
            {"ID":3,"Name":"已结束"},
        ],
    },
    methods: {
        doSelectProject: function(ID,Name) {
            var that = this;
            that.ProjectName=Name;
            that.form.ProjectID=ID;
            that.form.ProjectIDs=[];
            that.form.ProjectIDs.push(ID);
        },
        doSelectStatus: function (ID) {
            var that = this;
            var index = that.form.StatusIDs.indexOf(ID);
            if (index > -1) {
                that.form.StatusIDs.splice(index, 1);
            } else {
                that.form.StatusIDs.push(ID);
            }
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
            if (type == 2 && that.showsearchmonth) {
                setTimeout(function () {
                    that.initPicker();
                }, 100);
            }
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doSearch: function () {
            var that = this;
            that.doHideSearch();
            that.get_data();
        },
         //筛选数据
         getSearchParams: function () {
            var that = this;
            var options = {};
            options.action = "gettaskanalysisparams";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.projectList = data.projectList;
                    if(that.projectList!=undefined&&that.projectList.length>0&&that.form.ProjectID<=0){
                        that.doSelectProject(that.projectList[0].ID,that.projectList[0].Name); 
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        get_data: function() {
            var that = this;
            that.is_searching= true,
            that.can_scroll = false;
            var options = {};
            options.action = "getanalysisequipmentlist";
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.PlanType = 1;
            options.projectIds = JSON.stringify(that.form.ProjectIDs);
            options.startTime = that.form.startTime;
            options.endTime = that.form.endTime;
            options.statusIds = JSON.stringify(that.form.StatusIDs);

            // console.log(JSON.stringify(options));
            ns.post(options, function(succeed, data, err) {
                // console.log(JSON.stringify(data));
                that.is_searching= false;
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;

                    } else {
                        that.list = that.list.concat(data.list);
                    }
                    app.current_item_length = that.list.length
                    
                } else if (err) {
                    // that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        open_list: function(item) {
            var that = this;
            var url = 'equipment_list_frm_task';
            var title = item.DevicePlanName;
            ns.openWin(url, "设备信息", {
                id: item.ID,
                TaskType:1,//设备巡检统计任务 跳转设备列表
            });
        },
        doClickDay: function(dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentWeek = dateRangeUtil.getCurrentWeek();
                startDate = dateRangeUtil.getDateString(currentWeek[0]);
                endDate = dateRangeUtil.getDateString(currentWeek[1]);
                that.form.activeDateDesc = '本周';
            }
            if (dataValue == 3) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        doReset: function(type) {
            var that = this;
            if (type == 2) {
                that.form.startTime = '';
                that.form.endTime = '';
                that.form.activeDate = 0;
                that.form.activeDateDesc='时间';
            } else {
                that.form.StatusIDs = [];
                that.form.ProjectIDs = [];
            }
        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            _self.picker.show(function (rs) {
                                _this.form.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.form.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    },
    
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;

    app.getSearchParams();
    app.form.ProjectID=api.pageParam.ProjectID || 0;
    app.ProjectName=api.pageParam.ProjectName || '';
    app.IsAnalysis=api.pageParam.IsAnalysis || false;
    var startMonth=api.pageParam.startMonth || '';
    if(startMonth){
      app.form.startTime=moment(startMonth).startOf("month").format("YYYY-MM-DD");
      app.form.endTime=moment(startMonth).endOf("month").format("YYYY-MM-DD");
    }
    else{
        app.doClickDay(3);  
    }
    app.form.ProjectIDs=[];
    app.form.ProjectIDs.push(app.form.ProjectID);
    app.doSelectStatus(0);
   
    setTimeout(() => {
        app.doSearch();
    }, 500);
    ns.init_pull();
};

